package com.ruoyi.web.controller.common;

import com.ruoyi.api.MqttConsumer;
import com.ruoyi.api.WebSocketService;
import com.ruoyi.common.utils.websocket.WebSocketUtils;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/websocket")
public class WebSocketController {

//    @Autowired
//    private MqttSender mqttSender;


    @GetMapping("/testWebsocket")
    public void testWebsocket(@Param("uid") String uid, @Param("msg") String msg)
    {
        WebSocketUtils.sendMessage(uid, msg);
    }

    /**
     * 测试推送消息
     */
    @ResponseBody
    @GetMapping(value = "/push")
    public Object push(@Param("topic") String topic, @Param("msg") String msg) {
        MqttConsumer.publish(topic, msg);
        return "测试发布主题成功";
    }

    /**
     * 测试接收消息
     */
    @ResponseBody
    @GetMapping(value = "/subscribe")
    public Object subscribe(@Param("topic") String topic, @Param("qus") int qus) {
        MqttConsumer.subscribe(topic, qus);
        return "订阅主题"+topic+"成功";
    }

}
